package com.loan.controller;
import java.util.Date;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.loan.common.CONST;
import com.loan.model.Data;
import com.loan.model.Slider;
import com.loan.service.SliderService;
import com.loan.util.Json;
import com.loan.util.Str;
 
public class SliderController extends Controller {
	private static SliderService slider = new SliderService();
	
	/**
	 *  获取列表列表
	 */
	public void list() {
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		renderJson(Json.ok("数据获取成功！", slider.list(page, size, Data.flat(getParaMap()))));
	}
	
	/**
	 * 编辑轮播图信息
	 */
	public void edit() {
		int id = getParaToInt("id",0);
		Slider slider = id>0 ? Slider.dao.findById(id) : new Slider();
		slider.setName(getPara("name"));
		slider.setLink(getPara("link"));
		slider.setPic(getPara("pic"));
		slider.setDsporder(getParaToInt("dsporder",0));
		slider.setStatus(getPara("status"));
		slider.setContents(getPara("contents"));
		String link = getPara("link"); 
		
		if(id>0 ? slider.update() : slider.setCreateTime(new Date()).save()){
			if(Str.nb(link)) {
				slider.setLink(link);
			}else {
				slider.setLink(CONST.URL+"/#/Activity?id="+slider.getId()); 
			}
			renderJson(Json.ok("轮播图信息编辑成功！")); 
		}else{
			renderJson(Json.error("轮播图信息编辑失败！"));
		}
	}
	
	/**
	 *  获取轮播图信息
	 */
	@Clear
	public void info() {
		getResponse().setHeader("Access-Control-Allow-Origin","*"); 
		renderJson(Json.ok("数据获取成功！", Slider.dao.findById(getParaToInt("id",0))));
	}
	
	/**
	 *  轮播图上线/下线
	 */
	public void status() {
		Slider s = Slider.dao.findById(getParaToInt("id",0));
		if(s!=null){
			s.setStatus(getPara("status")).update();
			renderJson(Json.ok("状态修改成功！"));
		}
	}
	
	/**
	 * 公告删除
	 */
	public void del() {
		Slider s = Slider.dao.findById(getParaToInt("id",0));
		if(s!=null){
			s.delete();
			renderJson(Json.ok("删除成功！"));
		}
	}
}
 